package Alex.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;


public class InternalPage extends AnyPage {

	public InternalPage(PageManager pages) {
		super(pages);
	}
	
  public InternalPage ensurePageLoaded() {
    super.ensurePageLoaded();
    wait.until(presenceOfElementLocated(By.cssSelector("nav")));
    return this;
  }
  
  @FindBy(css = "#q")
  private WebElement searchField;
  
  @FindBy(css = "a[href='./?go=add']")
  private WebElement addMovieButton;
  
  @FindBy(css = ".title")
  private List <WebElement> moviesList = driver.findElements(By.cssSelector(".title"));
  
  @FindBy(css = ".content")
  private List <WebElement> noMovies = driver.findElements(By.cssSelector(".content"));
  
  @FindBy(css = "nav a[href $= '?go=profile']")
  private WebElement userProfileLink;

  @FindBy(css = "nav a[href $= '?go=users']")
  private WebElement userManagementLink;

  @FindBy(css = "nav a[onclick $= '?logout']")
  private WebElement logoutLink;
  
  public UserProfilePage clickUserProfilePage() {
    userProfileLink.click();
    return pages.userProfilePage;
  }

  public InternalPage setSearchFilter(String text){
	  searchField.clear();
	  searchField.sendKeys(text);
	  searchField.click();
	  searchField.sendKeys(Keys.ENTER);
	  return this;
  }
  
  public AddMoviePage clickAddMovieButton(){
	  addMovieButton.click();
	  return pages.addMoviePage;
  }
  
  public EditMoviePage selectMovie(){
	  if (moviesList.size()>0){
		  moviesList.get(0).click();
	  }
	  return pages.editMoviePage;
  }
  
  public boolean noMoviesFound(){
	  return !noMovies.isEmpty();
  }
  
  
  public boolean moviesFound(){
	  return noMovies.isEmpty();
  }
  
  public UserManagementPage clickUserManagementLink() {
    userManagementLink.click();
    return pages.userManagementPage;
  }
  
  public LoginPage clickLogoutLink() {
    logoutLink.click();
    wait.until(alertIsPresent()).accept();
    return pages.loginPage;
  }
}
